/*     */ package com.services.bo;
/*     */ 
/*     */ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import tools.MyLogger;

import com.services.sf.ApprovalCode;
import com.services.sf.FullTransaction;
import com.services.sf.InstallmentTransaction;
import com.services.sf.OutputListTransactionIPAY;
import com.services.sf.OutputPaymentIPAY;
import com.services.sf.Reason;
import com.services.sf.StatusCode;

import da.factory.DBEngine;
/*     */ 
/*     */ public class KlikPayment
/*     */ {
/*     */   DBEngine db;
/*     */ 
/*     */   public KlikPayment()
/*     */   {
/*     */     try
/*     */     {
/*  34 */       MyLogger.getInstance("bca.log");
/*  35 */       MyLogger.info("init KlikPayment..");
/*  36 */       this.db = new DBEngine();
/*  37 */       if (!this.db.connect())
/*     */       {
/*  39 */         System.out.println("database is not connect...");
/*  40 */         MyLogger.info("ERROR: database is not connect...");
/*     */       }
/*     */       else {
/*  43 */         MyLogger.info("INFO: database is connect...");
/*     */       }
/*     */     } catch (Exception e) {
/*  46 */       System.out.println(e.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public OutputPaymentIPAY getPayment(String klikPayCode, String transactionNo, String transactionDate, String currency, String totalAmount, String payType, ApprovalCode approvalCode, String authKey, String additionalData)
/*     */   {
/*  52 */     OutputPaymentIPAY output = new OutputPaymentIPAY();
/*     */     try
/*     */     {
/*  55 */       output.setKlikPayCode(klikPayCode);
/*  56 */       output.setTransactionDate(transactionDate);
/*  57 */       output.setTransactionNo(transactionNo);
/*  58 */       output.setCurrency(currency);
/*  59 */       output.setTotalAmount(totalAmount);
/*  60 */       output.setPayType(payType);
/*  61 */       output.setMerchantId(getMerchantId(transactionNo));
/*  62 */       System.out.println(output.getMerchantId());
/*  63 */       ApprovalCode[] approval = new ApprovalCode[1];
/*  64 */       approval[0] = new ApprovalCode();
/*  65 */       System.out.println("fulltransaction: " + approvalCode.getFullTransaction());
/*  66 */       System.out.println("installmenttransaction: " + approvalCode.getInstallmentTransaction());
/*  67 */       approval[0].setFullTransaction(approvalCode.getFullTransaction());
/*  68 */       approval[0].setInstallmentTransaction(approvalCode.getInstallmentTransaction());
/*  69 */       output.setApprovalCode(approval);
/*  70 */       output.setStatus("01");
/*  71 */       Reason[] reason = new Reason[1];
/*  72 */       reason[0] = new Reason();
/*  73 */       reason[0].setIndonesian("Transaksi anda tidak dapat diproses");
/*  74 */       reason[0].setEnglish("Your transaction can not be processed");
/*  75 */       output.setReason(reason);
/*  76 */       output.setAdditionalData(additionalData);
/*  77 */       System.out.println("start->getOututPayment [" + transactionNo + "," + klikPayCode + "]");
/*  78 */       OutputPaymentIPAY tmpOut = getOutputPayment(transactionNo, klikPayCode);
/*  79 */       System.out.println("finish->getOututPayment [" + transactionNo + "," + klikPayCode + "]");
/*  80 */       if (tmpOut == null)
/*     */       {
/*  82 */         System.out.println("getOutputPayment->null");
/*  83 */         return output;
/*     */       }
/*     */ 
/*  87 */       if (!tmpOut.getCurrency().equals(currency))
/*     */       {
/*  89 */         System.out.println("getOutputPayment->currency is not equal");
/*  90 */         return output;
/*     */       }
/*     */ 
/* 101 */       String tmpTransDate = tmpOut.getTransactionDate();
/*     */ 
/* 103 */       if (payType.equals("01"))
/*     */       {
/* 105 */         if (!isFullTransaction(transactionNo)) {
/* 106 */           return output;
/*     */         }
/*     */ 
/* 109 */         double amt = getAmountFullTranction(transactionNo);
/* 110 */         double amt1 = Double.parseDouble(totalAmount);
/* 111 */         if (amt != amt1)
/*     */         {
/* 113 */           System.out.println("getOutputPayment->amount is not equal[" + amt + "][" + amt1 + "]");
/* 114 */           return output;
/*     */         }
/* 116 */         output.setMerchantId("096081141");
/*     */       }
/*     */ 
/* 119 */       if (payType.equals("02"))
/*     */       {
/* 121 */         if (!isInstallment(transactionNo)) {
/* 122 */           return output;
/*     */         }
/*     */ 
/* 125 */         double amt = getAmountInstallmentTranction(transactionNo);
/* 126 */         double amt1 = Double.parseDouble(totalAmount);
/* 127 */         if (amt != amt1)
/*     */         {
/* 129 */           System.out.println("getOutputPayment->amount is not equal[" + amt + "][" + amt1 + "]");
/* 130 */           return output;
/*     */         }
/*     */       }
/*     */ 
/* 134 */       if (payType.equals("03"))
/*     */       {
/* 136 */         if ((!isInstallment(transactionNo)) || (!isFullTransaction(transactionNo))) {
/* 137 */           return output;
/*     */         }
/*     */ 
/* 140 */         double amtFull = getAmountFullTranction(transactionNo);
/* 141 */         double amtInstall = getAmountInstallmentTranction(transactionNo);
/* 142 */         double total = amtFull + amtInstall;
/* 143 */         double amt1 = Double.parseDouble(totalAmount);
/* 144 */         if (total != amt1)
/*     */         {
/* 146 */           System.out.println("getOutputPayment->amount is not equal[full:" + amtFull + ",install: " + amtInstall + "][" + amt1 + "]");
/* 147 */           return output;
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 152 */       System.out.println("Request Http");
/* 153 */       StatusCode statusCode = getHttpRequest(transactionNo, klikPayCode);
/* 154 */       if (statusCode != null)
/*     */       {
/* 156 */         output.setStatus(statusCode.getStatusCode());
/* 157 */         reason[0].setIndonesian(statusCode.getReasonIndonesian());
/* 158 */         reason[0].setEnglish(statusCode.getReasonEnglish());
/*     */       }
/* 160 */       if (isTimeOut(tmpTransDate))
/*     */       {
/* 162 */         output.setStatus("01");
/* 163 */         reason[0].setIndonesian("Transaksi anda telah kedaluwarsa");
/* 164 */         reason[0].setEnglish("Your transaction has been expired");
/* 165 */         output.setReason(reason);
/* 166 */         return output;
/*     */       }
/*     */ 
/* 172 */       String clearKey = "KlikPayDevLegend";
/* 173 */       GenerateAuthKey client = new GenerateAuthKey();
/* 174 */       String keyId = client.toHexString(clearKey.getBytes());
/* 175 */       System.out.println("keyId : " + keyId);
/*     */ 
/* 177 */       String data = String.format("%-10s", new Object[] { klikPayCode }).replace(' ', '0') + String.format("%-18s", new Object[] { transactionNo }).replace(' ', 'A') + String.format("%-5s", new Object[] { currency }).replace(' ', '1') + tmpTransDate + String.format("%-32s", new Object[] { keyId }).replace(' ', 'E');
/*     */       try
/*     */       {
/* 184 */         String tmpAuthKey = client.generateAuthKey(data, keyId);
/* 185 */         System.out.println("authKey : " + tmpAuthKey);
/* 186 */         if (!tmpAuthKey.equals(authKey))
/*     */         {
/* 188 */           output.setStatus("01");
/* 189 */           reason[0].setIndonesian("Transaksi telah dibatalkan");
/* 190 */           reason[0].setEnglish("Transaction has been canceled");
/* 191 */           output.setReason(reason);
/* 192 */           updateTransStatus(output);
/*     */         }
/*     */       } catch (Exception ex) {
/* 195 */         ex.printStackTrace();
/*     */       }
/*     */ 
/* 198 */       return output;
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 202 */       MyLogger.info("getPayment()->ERROR: " + ex.getLocalizedMessage());
/* 203 */       System.out.println("getPayment()->ERROR: " + ex.getLocalizedMessage());
/*     */     }
/* 205 */     return output;
/*     */   }
/*     */ 
/*     */   private boolean isTimeOut(String transdate)
/*     */   {
/* 210 */     SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
/* 211 */     Date d1 = null;
/* 212 */     Date d2 = null;
/*     */     try {
/* 214 */       d1 = format.parse(transdate);
/* 215 */       d2 = new Date();
/*     */     } catch (ParseException e) {
/* 217 */       e.printStackTrace();
/*     */     }
/* 219 */     long diff = d2.getTime() - d1.getTime();
/* 220 */     long diffSeconds = diff / 1000L;
/* 221 */     long diffMinutes = diff / 60000L;
/* 222 */     long diffHours = diff / 3600000L;
/*     */ 
/* 225 */     return diffHours > 1L;
/*     */   }
/*     */ 
/*     */   private OutputPaymentIPAY getOutputPayment(String transactionNo, String klikpaycode)
/*     */   {
/* 233 */     OutputPaymentIPAY output = null;
/* 234 */     String strSQL = "SELECT currency,transactionDate FROM inquery where transactionNo='" + transactionNo + "' and klikPayCode='" + klikpaycode + "'";
/* 235 */     System.out.println(strSQL);
/* 236 */     MyLogger.info(strSQL);
/* 237 */     Vector rows = this.db.getData(strSQL);
/* 238 */     int count = rows.size();
/* 239 */     System.out.println("getOutputPayment->count: " + count);
/* 240 */     if (count > 0)
/*     */     {
/* 242 */       output = new OutputPaymentIPAY();
/* 243 */       output.setKlikPayCode(klikpaycode);
/* 244 */       output.setTransactionNo(transactionNo);
/* 245 */       for (int i = 0; i < count; i++)
/*     */       {
/* 247 */         Vector v = (Vector)rows.get(i);
/* 248 */         output.setCurrency(v.get(0).toString());
/* 249 */         output.setTransactionDate(v.get(1).toString());
/*     */       }
/*     */     }
/* 252 */     return output;
/*     */   }
/*     */ 
/*     */   private String getTransactionDate(String transactionNo, String klikpaycode) {
/* 256 */     String transactionDate = "";
/*     */ 
/* 258 */     String strSQL = "SELECT transactionDate FROM inquery where transactionNo='" + transactionNo + "' and klikPayCode='" + klikpaycode + "'";
/* 259 */     System.out.println(strSQL);
/* 260 */     MyLogger.info(strSQL);
/* 261 */     Vector rows = this.db.getData(strSQL);
/* 262 */     int count = rows.size();
/* 263 */     if (count > 0)
/*     */     {
/* 265 */       for (int i = 0; i < count; i++)
/*     */       {
/* 267 */         Vector v = (Vector)rows.get(i);
/* 268 */         transactionDate = v.get(0).toString();
/*     */       }
/*     */     }
/* 271 */     return transactionDate;
/*     */   }
/*     */ 
/*     */   private StatusCode getHttpRequest(String transactionNo, String klikpaycode) {
/* 275 */     StatusCode statusCode = null;
/*     */     try
/*     */     {
/* 279 */       String data = URLEncoder.encode("transactionno", "UTF-8") + "=" + URLEncoder.encode(transactionNo, "UTF-8");
/* 280 */       data = data + "&" + URLEncoder.encode("klikpaycode", "UTF-8") + "=" + URLEncoder.encode(klikpaycode, "UTF-8");
/* 281 */       System.out.println("encode http request: " + data);
/*     */ 
/* 284 */       String m_url = "http://www.legendaris.com/processBCA/processBCA.php";
/* 285 */       System.out.println("url: " + m_url);
/* 286 */       URL url = new URL(m_url);
/* 287 */       URLConnection conn = url.openConnection();
/* 288 */       conn.setDoOutput(true);
/* 289 */       OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
/* 290 */       wr.write(data);
/* 291 */       wr.flush();
/*     */ 
/* 294 */       BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
/*     */ 
/* 296 */       StringBuffer response = new StringBuffer();
/* 297 */       ParsingXML parse = new ParsingXML();
/*     */       String line;
/* 298 */       while ((line = rd.readLine()) != null)
/*     */       {
/* 300 */         System.out.println("line: " + line);
/* 301 */         response.append(line);
/*     */       }
/* 303 */       System.out.println("response: " + response.toString());
/* 304 */       parse.parse(response.toString());
/* 305 */       if (statusCode == null)
/* 306 */         statusCode = new StatusCode();
/* 307 */       statusCode.setStatusCode(parse.getStatusCode());
/* 308 */       statusCode.setReasonEnglish(parse.getReasonEnglish());
/* 309 */       statusCode.setReasonIndonesian(parse.getReasonIndonesia());
/* 310 */       wr.close();
/* 311 */       rd.close();
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 315 */       System.out.println("getHttpRequest()->ERROR: " + ex.getLocalizedMessage());
/* 316 */       MyLogger.info("getHttpRequest()->ERROR: " + ex.getLocalizedMessage());
/*     */     }
/* 318 */     return statusCode;
/*     */   }
/*     */ 
/*     */   private String getMerchantId(String transactionNo)
/*     */   {
/* 323 */     String merchantId = "";
/*     */ 
/* 325 */     String strSQL = "SELECT merchantId FROM installmenttransaction where transactionNo='" + transactionNo + "'";
/* 326 */     System.out.println(strSQL);
/* 327 */     MyLogger.info(strSQL);
/* 328 */     Vector rows = this.db.getData(strSQL);
/* 329 */     int count = rows.size();
/* 330 */     if (count > 0)
/*     */     {
/* 332 */       for (int i = 0; i < count; i++)
/*     */       {
/* 334 */         Vector v = (Vector)rows.get(i);
/* 335 */         merchantId = v.get(0).toString();
/*     */       }
/*     */     }
/* 338 */     return merchantId;
/*     */   }
/*     */ 
/*     */   public OutputListTransactionIPAY getListInquery(String klikPayCode, String transactionNo, String signature, String additionalData) {
/* 342 */     OutputListTransactionIPAY tmpOut = new OutputListTransactionIPAY();
/*     */ 
/* 344 */     tmpOut.setKlikpayCode(klikPayCode);
/* 345 */     tmpOut.setTransactionNo(transactionNo);
/* 346 */     String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
/* 347 */     double amount = 0.0D;
/*     */     try
/*     */     {
/* 350 */       String strSQL = "SELECT * FROM inquery where klikPayCode='" + klikPayCode + "' and transactionNo='" + transactionNo + "'";
/* 351 */       System.out.println(strSQL);
/* 352 */       MyLogger.info("getListInquery->" + strSQL);
/* 353 */       Vector rows = this.db.getData(strSQL);
/* 354 */       int count = rows.size();
/* 355 */       for (int i = 0; i < count; i++)
/*     */       {
/* 357 */         Vector v = (Vector)rows.get(i);
/* 358 */         tmpOut.setKlikpayCode(v.get(0).toString());
/* 359 */         tmpOut.setTransactionNo(v.get(1).toString());
/* 360 */         tmpOut.setCurrency(v.get(3).toString());
/*     */         try {
/* 362 */           tmpOut.setMiscFee(v.get(4).toString());
/*     */         }
/*     */         catch (Exception ex) {
/* 365 */           tmpOut.setMiscFee("0.00");
/*     */         }
/*     */         try
/*     */         {
/* 369 */           tmpOut.setAdditionalData(v.get(5).toString());
/*     */         }
/*     */         catch (Exception ex) {
/* 372 */           tmpOut.setAdditionalData(null);
/*     */         }
/*     */ 
/* 375 */         date = v.get(6).toString();
/*     */       }
/*     */ 
/* 379 */       strSQL = "SELECT * FROM fulltransaction where transactionNo='" + transactionNo + "'";
/* 380 */       System.out.println("getListInquery->" + strSQL);
/* 381 */       MyLogger.info(strSQL);
/* 382 */       rows = this.db.getData(strSQL);
/* 383 */       count = rows.size();
/* 384 */       if (count > 0)
/*     */       {
/* 386 */         FullTransaction[] fullTrans = new FullTransaction[count];
/* 387 */         for (int i = 0; i < count; i++)
/*     */         {
/* 389 */           Vector v = (Vector)rows.get(i);
/* 390 */           fullTrans[i] = new FullTransaction();
/* 391 */           String sAmt = v.get(1).toString();
/* 392 */           double amt = Double.parseDouble(sAmt);
/* 393 */           amount += amt;
/* 394 */           fullTrans[i].setAmount(sAmt);
/* 395 */           fullTrans[i].setDescription(v.get(2).toString());
/*     */         }
/* 397 */         tmpOut.setFullTransaction(fullTrans);
/*     */       }
/*     */ 
/* 400 */       strSQL = "SELECT * FROM installmenttransaction where transactionNo='" + transactionNo + "'";
/* 401 */       System.out.println(strSQL);
/* 402 */       MyLogger.info("getListInquery->" + strSQL);
/* 403 */       rows = this.db.getData(strSQL);
/* 404 */       count = rows.size();
/* 405 */       if (count > 0)
/*     */       {
/* 407 */         InstallmentTransaction[] installmentTrans = new InstallmentTransaction[count];
/* 408 */         for (int i = 0; i < count; i++)
/*     */         {
/* 410 */           Vector v = (Vector)rows.get(i);
/* 411 */           installmentTrans[i] = new InstallmentTransaction();
/* 412 */           installmentTrans[i].setItemName(v.get(1).toString());
/* 413 */           installmentTrans[i].setQuantity(Integer.parseInt(v.get(2).toString()));
/* 414 */           String sAmt = v.get(3).toString();
/* 415 */           double amt = Double.parseDouble(sAmt);
/* 416 */           amount += amt;
/* 417 */           installmentTrans[i].setAmount(sAmt);
/* 418 */           installmentTrans[i].setTenor(v.get(4).toString());
/* 419 */           installmentTrans[i].setCodePlan(v.get(5).toString());
/* 420 */           installmentTrans[i].setMerchantId(v.get(6).toString());
/*     */         }
/* 422 */         tmpOut.setInstallmentTransaction(installmentTrans);
/*     */       }
/*     */ 
/* 426 */       String clearKey = "KlikPayDevLegend";
/* 427 */       GenerateAuthKey client = new GenerateAuthKey();
/* 428 */       String keyId = client.toHexString(clearKey.getBytes());
/* 429 */       System.out.println("keyId : " + keyId);
/* 430 */       String mysignature = client.generateSignature(klikPayCode, date, transactionNo, String.valueOf(amount), tmpOut.getCurrency(), keyId);
/* 431 */       System.out.println("mysignature: " + mysignature);
/* 432 */       if (!mysignature.equals(signature))
/*     */       {
/* 434 */         System.out.println("Signature: not valid: " + mysignature + " vs " + signature);
/* 435 */         tmpOut.setFullTransaction(null);
/* 436 */         tmpOut.setInstallmentTransaction(null);
/*     */       }
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 441 */       tmpOut.setAdditionalData(ex.getLocalizedMessage());
/* 442 */       System.out.println(ex.getLocalizedMessage());
/* 443 */       MyLogger.info("ERROR getListInquery->" + ex.getLocalizedMessage());
/*     */     }
/* 445 */     return tmpOut;
/*     */   }
/*     */ 
/*     */   private boolean isInstallment(String transactionNo)
/*     */   {
/* 450 */     String strSQL = "SELECT * FROM installmenttransaction where transactionNo='" + transactionNo + "'";
/* 451 */     Vector rows = this.db.getData(strSQL);
/* 452 */     int count = rows.size();
/*     */ 
/* 455 */     return count > 0;
/*     */   }
/*     */ 
/*     */   private double getAmountFullTranction(String transactionNo)
/*     */   {
/* 462 */     double amount = 0.0D;
/* 463 */     String strSQL = "SELECT amount FROM fulltransaction where transactionNo='" + transactionNo + "'";
/* 464 */     Vector rows = this.db.getData(strSQL);
/* 465 */     int count = rows.size();
/* 466 */     if (count > 0)
/*     */     {
/* 468 */       for (int i = 0; i < count; i++)
/*     */       {
/* 470 */         Vector v = (Vector)rows.get(i);
/* 471 */         String sAmt = v.get(0).toString();
/* 472 */         double amt = Double.parseDouble(sAmt);
/* 473 */         amount += amt;
/*     */       }
/*     */     }
/* 476 */     return amount;
/*     */   }
/*     */ 
/*     */   private double getAmountInstallmentTranction(String transactionNo)
/*     */   {
/* 481 */     double amount = 0.0D;
/* 482 */     String strSQL = "SELECT amount FROM installmenttransaction where transactionNo='" + transactionNo + "'";
/* 483 */     Vector rows = this.db.getData(strSQL);
/* 484 */     int count = rows.size();
/* 485 */     if (count > 0)
/*     */     {
/* 487 */       for (int i = 0; i < count; i++)
/*     */       {
/* 489 */         Vector v = (Vector)rows.get(i);
/* 490 */         String sAmt = v.get(0).toString();
/* 491 */         double amt = Double.parseDouble(sAmt);
/* 492 */         amount += amt;
/*     */       }
/*     */     }
/* 495 */     return amount;
/*     */   }
/*     */ 
/*     */   private boolean isFullTransaction(String transactionNo)
/*     */   {
/* 500 */     String strSQL = "SELECT * FROM fulltransaction where transactionNo='" + transactionNo + "'";
/* 501 */     Vector rows = this.db.getData(strSQL);
/* 502 */     int count = rows.size();
/*     */ 
/* 505 */     return count > 0;
/*     */   }
/*     */ 
/*     */   private boolean updateTransStatus(OutputPaymentIPAY output)
/*     */   {
/* 512 */     System.out.println("transNo: " + output.getTransactionNo());
/* 513 */     System.out.println("klikpayCde: " + output.getTransactionNo());
/*     */ 
/* 515 */     String strSQL = "INSERT INTO transstatus (transactionno, klikpaycode, statuscode, reasonindonesia, reasonenglish, transactiondate) values (?,?,?,?,?,?)";
/* 516 */     MyLogger.info(strSQL);
/*     */     try {
/* 518 */       PreparedStatement p = this.db.getStatement(strSQL);
/* 519 */       p.setString(1, output.getTransactionNo());
/* 520 */       p.setString(2, output.getKlikPayCode());
/* 521 */       p.setString(3, output.getStatus());
/* 522 */       p.setString(4, output.getReason()[0].getIndonesian());
/* 523 */       p.setString(5, output.getReason()[0].getEnglish());
/* 524 */       p.setString(6, output.getTransactionDate());
/*     */ 
/* 527 */       return this.db.execute(p);
/*     */     }
/*     */     catch (SQLException e) {
/* 530 */       MyLogger.info(e.getLocalizedMessage());
/* 531 */       System.out.println("updateTransStatus()->ERROR: " + e.getLocalizedMessage());
/*     */     }
/* 533 */     return false;
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 538 */     ParsingXML parse = new ParsingXML();
/* 539 */     String response = "<?xml version='1.0' encoding='ISO-8859-1'?><response><statuscode>00</statuscode><reasonindonesian>Indonesia Raya</reasonindonesian><reasonenglish>Time expired</reasonenglish></response>";
/* 540 */     parse.parse(response.toString());
/*     */   }
/*     */ }

/* Location:           F:\BCA.jar
 * Qualified Name:     com.services.bo.KlikPayment
 * JD-Core Version:    0.6.0
 */